#include "Main.h"
#include <windows.h>
using namespace std;

#define HINSTANCE HINSTANCE
#define HWND HWND

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR str, int nCmdShow);
LRESULT CALLBACK WndProg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR str, int nCmdShow)
{
	//class
	WNDCLASSEXW wc;
	ZeroMemory(&wc, sizeof(wc));
	wc.hInstance = hInst;
	wc.cbSize = sizeof(wc);
	wc.lpszClassName = L"stdclass";
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DROPSHADOW;
	wc.hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_MAIN_ICON));
	wc.hIconSm = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_SMALL));
	wc.lpfnWndProc = WndProg;
	if (!RegisterClassExW(&wc))
		return E_FAIL;
	/////////////////////////////////////////////
	//init
	InitApp();
	/////////////////////////////////////////////
	//cicle
	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	while (msg.message != WM_QUIT)
	{
        if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
		{
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
		}
	}
	/////////////////////////////////////////////
	//exit
	ReleaseApp();
	UnregisterClassW(L"stdclass", hInst);
	return msg.wParam;
}

LRESULT CALLBACK WndProg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
	case WM_CLOSE:
		if (EventCloseWindow(hWnd))
		{
			PostQuitMessage(0);
		}
		break;
	case WM_COMMAND:
        EventCommand(hWnd, wParam);
		break;
	default:
		return DefWindowProcW(hWnd, msg, wParam, lParam);
    }
    return 0;
}
